package com.xt.core.service.hibernate;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xt.core.service.BasicService;
import com.xt.core.dao.BasicDao;
import com.xt.core.exception.ObjectCanNotSaveException;
import com.xt.core.helper.page.Pager;


/**
 * Generic Manager in hibernate implementaion that talks to BasicDao to CRUD POJOs.
 *
 * <p>Extend this interface if you want typesafe (no casting necessary) managers
 * for your domain objects.
 *
<p/>
 * <p>To register this class in your Spring context file, use the following XML.
 * <pre>
 *     &lt;bean id="userManager" class="com.xt.core.service.impl.BasicServiceImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="com.xt.core.dao.impl.BadicDaoHibernate"&gt;
 *                 &lt;constructor-arg value="com.xt.dummy.data.User"/&gt;
 *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * <p/>
 *  
 * @author txiao
 * @version Revision: 1.0, Date: 23.09.2009
 * @param <T> a type variable
 * @param <PK> the primary key for that type
 */
public class BasicServiceImpl<T, PK extends Serializable> implements BasicService<T, PK>{
	
	/**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());
	
	protected BasicDao<T, PK> dao;
		
	public BasicServiceImpl() {

	}
	
    /**
     * GenericDao instance, set by constructor of child classes
     */

	public BasicServiceImpl(BasicDao<T, PK> dao) {
		this.dao = dao;
	}
	
    /**
     * {@inheritDoc}
     */
    public List<T> getAll() {
        return dao.getAll();
    }

    /**
     * {@inheritDoc}
     */
    public T get(PK id) {
        return dao.get(id);
    }

    /**
     * {@inheritDoc}
     */
    public boolean exists(PK id) {
        return dao.exists(id);
    }

    /**
     * {@inheritDoc}
     */
    public T save(T object) {
        return dao.save(object);
    }

    /**
     * {@inheritDoc}
     */
    public void remove(PK id) {
        dao.remove(id);
    }
	
	
	/*
     * Add your function here
     */
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public void saveorupdate(T object) throws ObjectCanNotSaveException{
		dao.saveorupdate(object);
	}
	
	public int getTotalRows(){
		return dao.getTotalRows();
	}
	
	public int getTotalRows(Set<LinkedHashMap<String, Object>> queryParams){
		return dao.getTotalRows(queryParams);
	}
	
	public long getTotalRows(String queryString){
		return dao.getTotalRows(queryString);
	}
	
	public List<T> getAll(Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order){
		return dao.getAll(queryParams, order);
	}
	
	public List<T> getOnePage(int pageSize, int startRow){
		return dao.getOnePage(pageSize, startRow);
	}
	
	public List<T> getOnePage(Pager pager){
		return dao.getOnePage(pager);
	}
	
	public List<T> getOnePage(int pageSize, int startRow, Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order){
		return dao.getOnePage(pageSize, startRow, queryParams, order);
	}
	
	public List<T> getOnePage(Pager pager, Set<LinkedHashMap<String, Object>> queryParams, LinkedHashMap<String, String> order){
		return dao.getOnePage(pager,queryParams,order);
	}
	
	public List<T> getOnePage( String queryString, int pageSize, int startRow){
		return dao.getOnePage(queryString, pageSize, startRow);
	}
	
	public List<T> getOnePage( String queryString, Pager pager){
		return dao.getOnePage(queryString, pager);
	}
}
